import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
declare var echarts: any;
declare var Viewer: any;
@Component({
	selector: 'sa-drawings-match-rule',
	templateUrl: './drawings-match-rule.component.html',
	styleUrls: ['./drawings-match-rule.component.css']
})
export class DrawingsMatchRuleComponent implements OnInit {
	tableData = []; //table数据    侧挂链条
	totalCount: number = 0; //分页总条数   侧挂链条
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	tableParam: any = {
		carType: '',
		useTypeL2: '',
		componentType: '',
		userName: '',//录入人员
		startTime: '',
		endTime: '',
	}
	userList: any = [];//用户数据List
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}
	ngOnInit() {
		this.initAllUser().then(() => {
			this.selectUserName();
		});
		this.load();
		this.partTypeChange(1);
	}
	ngOnDestroy() {
		document.onclick = null;
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.tableParam.startTime = time.startTime;
		this.tableParam.endTime = time.endTime;
	}
	//擦除用户所选值
	delUserName() {
		this.tableParam.userName = '';
		//采用简单粗暴的方法清除;官方推荐的方法失效
		var str = $("<span class='select2-selection__placeholder'>请选择</span>");
		$('#userNameSelect2').val("");
		$("#select2-userNameSelect2-container").empty();
		$("#select2-userNameSelect2-container").attr("title", "");
		$("#select2-userNameSelect2-container").prepend(str);
	}
	//初始化select2 选项
	selectUserName() {
		var that = this;
		System.import('script-loader!select2/dist/js/select2.min.js').then(() => {
			System.import('assets/js/select2-zh-CN.js').then(() => {
				$("#userNameSelect2").select2({
					language: "zh-CN",
					placeholder: "请选择",
					allowClear: true,
					data: this.userList,//为Select添加数据
					templateResult: this.formatRepo
				}).on("select2:select", function (e) {
					var data = e.params.data;
					that.tableParam.userName = data.text;
				});
			})
		})
	}
	//select2 格式回调函数
	formatRepo(state) {
		if (!state.id) { return state.text };
		var $state = $('<span><i class="fa fa-user green"></i>&nbsp;&nbsp;' + state.text + '</span>');
		return $state;
	}
	//拉取
	initAllUser() {
		var that = this;
		return new Promise(function (resolve, reject) {
			that.jsonApiService.http("user/getUserList.html", null).subscribe(data => {
				if (data.success) {
					var userList = data.data;
					var selectdata = [];
					for (var i = 0; i < userList.length; i++) {
						var obj = {
							id: userList[i].userName,
							text: userList[i].name,
						}
						selectdata[i] = obj;
					}
				}
				that.userList = selectdata;
				resolve();
			})
		})
	}
	//table刷新
	refresh() {
		this.load();
	}
	//表格搜索
	table_search() {
		this.curPage = 1;
		this.tableParam.searchKey = common.stringTrim(this.tableParam.searchKey);
		this.load();
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	//初始化加载list数据
	load() {
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				useTypeL2: this.tableParam.useTypeL2,
				componentType: this.tableParam.componentType,
				startTime: this.tableParam.startTime,
				endTime: this.tableParam.endTime,
				createForUser: this.tableParam.userName,//录入人员
			},
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("drawMatch/drawList.html", reqdata).subscribe(result => {
			this.tableData = result.exhibitDatas || [];
			this.totalCount = result.recordCount;
			//          this.tableData.map(item => item.useTypeL2Name = carTypeListObj.carDeatailUseList[item.useTypeL2])
		})
	}
	// 删除功能
	deleteRow(row) {
		this.jsonApiService.confirm_message('删除数据将永久删除，确认要删除?').then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("drawMatch/deleteById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
	/**
	 * 图纸匹配规则添加/编辑模块======================================================================= 
	 */
	// 侧挂链条
	T0213: any = [{
		"index": 1,
		"name": "横梁从左到右",
		"select": 0
	}, {
		"index": 2,
		"name": "挂齿顶到下底高",
		"select": 0
	}, {
		"index": 3,
		"name": "挂齿顶到下横梁底",
		"select": 0
	}, {
		"index": 4,
		"name": "压板底面到下底高",
		"select": 0
	}, {
		"index": 5,
		"name": "铁板左到右长度",
		"select": 0
	}]
	// 侧挂拉杆
	T0214: any = [{
		"index": 1,
		"name": "铁架内高",
		"select": 0
	}, {
		"index": 2,
		"name": "上横梁顶到下底高",
		"select": 0
	}, {
		"index": 3,
		"name": "挂齿顶到下底高",
		"select": 0
	}, {
		"index": 4,
		"name": "压板底到下底高",
		"select": 0
	}, {
		"index": 5,
		"name": "挂齿总长度",
		"select": 0
	}, {
		"index": 6,
		"name": "铁板左到右长度",
		"select": 0
	}]
	// 后挂双桶
	T0211: any = [{
		"index": 1,
		"name": "圆轴底到下端顶",
		"select": 0
	}, {
		"index": 2,
		"name": "左铁架内高",
		"select": 0
	}, {
		"index": 3,
		"name": "左挂齿到下端底",
		"select": 0
	}, {
		"index": 4,
		"name": "左挂齿总长度",
		"select": 0
	}, {
		"index": 5,
		"name": "下横梁高度",
		"select": 0
	}, {
		"index": 6,
		"name": "上横梁高度",
		"select": 0
	}, {
		"index": 7,
		"name": "左端到第2竖梁",
		"select": 0
	}, {
		"index": 8,
		"name": "第2竖梁到第3竖梁",
		"select": 0
	}, {
		"index": 9,
		"name": "第3竖梁到右端",
		"select": 0
	}, {
		"index": 10,
		"name": "底部宽度",
		"select": 0
	}, {
		"index": 11,
		"name": "顶部宽度",
		"select": 0
	}, {
		"index": 12,
		"name": "圆轴表面到外表面",
		"select": 0
	}, {
		"index": 13,
		"name": "圆轴表面到上横梁",
		"select": 0
	}, {
		"index": 14,
		"name": "圆轴表面到下横梁",
		"select": 0
	}]
	formType: string = 'new';
	matchRuleForm: any = {
		id: '',
		useTypeL2: '0213',
		componentType: '',
		drawDetailEntities: this.T0213,
	}
	useTypeL2Change() {
		switch (this.matchRuleForm.useTypeL2) {
			case '0213':
				this.matchRuleForm.drawDetailEntities = this.T0213;
				break;
			case '0214':
				this.matchRuleForm.drawDetailEntities = this.T0214;
				break;
			case '0211':
				this.matchRuleForm.drawDetailEntities = this.T0211;
				break;
			default:
				this.matchRuleForm.drawDetailEntities = [];
				break;
		}
		this.partTypeChange(2);
	}
	sensorLisSelected(lis) {
		if (this.formType == 'readonly') {
			return
		}
		console.log(this.matchRuleForm, 'this.matchRuleForm===11=')
		for (let item of this.matchRuleForm.drawDetailEntities) {
			if (item.name == lis.name) {
				if (item.select == 0) {
					item.select = 1;
				} else if (item.select == 1) {
					item.select = 0;
				}
				break
			}
		}
		console.log(this.matchRuleForm, 'this.matchRuleForm====')
	}
	// 新增匹配规则
	addRule(modal) {
		this.formType = 'new';
		for (let item of this.T0213) {
			item.select = 0;
		}
		for (let item of this.T0214) {
			item.select = 0;
		}
		for (let item of this.T0211) {
			item.select = 0;
		}
		this.matchRuleForm = {
			id: '',
			useTypeL2: '0213',
			componentType: '',
			drawDetailEntities: this.T0213,
		}
		this.partTypeChange(2);
		modal.show();
	}

	partTypeForm: any = {
		useTypeL2: '',
		useTypeL1: '0213',
		componentType: '',
		id: ''
	}
	partTableData = []; //table数据
	partTypeOption = [];
	popPartTypeOption = [];
	partTotalCount: number = 0; //分页总条数   侧挂链条
	partCurPage: number = 1; //分页当前页
	partPageSize: number = 10; //内置的分页每页条数
	// 零件类型
	partType(modal) {
		this.getPartTypeList();
		modal.show();
	}
	// 零件类型select
	partTypeChange(val) {
		let reqdata = `useTypeL2=${val == 1 ? this.tableParam.useTypeL2 : this.matchRuleForm.useTypeL2}`;
		this.jsonApiService.http("drawManage/componentType/componentTypeFind.html", reqdata).subscribe(result => {
			if (val == 1) {
				this.partTypeOption = result.data || [];
			} else {
				this.popPartTypeOption = result.data || [];
			}
		})
	}
	// 零件类型列表查询
	getPartTypeList() {
		let data = {
			pageSize: this.partPageSize,
			nowPage: this.partCurPage,
			parameters: {
				useTypeL2: this.partTypeForm.useTypeL2
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("drawManage/componentType/componentTypeList.html", reqdata).subscribe(result => {
			this.partTableData = result.exhibitDatas || [];
			this.partTotalCount = result.recordCount;
		})
	}
	// 翻页
	partTypePaginate(e) {
		this.partCurPage = e.page + 1;
		this.partPageSize = e.rows;
		this.getPartTypeList();
	}
	// 添加零件类型
	addPartType(modal1, modal2) {
		modal1.hide();
		modal2.show();
	}
	// 删除
	deletePartType(row) {
		this.jsonApiService.confirm_message('删除数据将永久删除，确认要删除?').then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("drawManage/componentType/componentTypeDelete.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.getPartTypeList();
				}
			})
		})
	}
	// 编辑
	editPartType(row, model1, model2) {
		this.partTypeForm.useTypeL1 = row.useTypeL2;
		this.partTypeForm.componentType = row.componentType;
		this.partTypeForm.id = row.id;
		model1.hide();
		model2.show();
	}
	// 关闭添加零件类型
	closeAddModal(modal1, modal2) {
		this.resetPartType();
		modal1.hide();
		modal2.show();
	}
	// 确定添加零件类型
	addPartTypeSubmit(modal1, modal2) {
		let reqdata = {
			useTypeL2: this.partTypeForm.useTypeL1,
			componentType: this.partTypeForm.componentType,
			id: this.partTypeForm.id,
		}
		this.jsonApiService.http_obj("drawManage/componentType/componentTypeAdd.html", reqdata).subscribe(data => {
			this.jsonApiService.message(data.message);
			if (data.success) {
				this.getPartTypeList();
				this.resetPartType();
				modal1.hide();
				modal2.show();
			}
		})
	}
	// 重置
	resetPartType() {
		this.partTypeForm.useTypeL2 = '';
		this.partTypeForm.useTypeL1 = '0213';
		this.partTypeForm.componentType = '';
		this.partTypeForm.id = '';
	}

	// 方案查看
	viewRow(row, modal) {
		this.formType = 'readonly';
		this.matchRuleForm = JSON.parse(JSON.stringify(row));
		modal.show();
	}
	// 编辑
	editRow(row, modal) {
		this.formType = 'update';
		this.matchRuleForm = JSON.parse(JSON.stringify(row));
		this.partTypeChange(2);
		modal.show();
	}
	// 规则表单提交
	matchRuleSubmit(modal) {
		if (!this.matchRuleForm.drawDetailEntities || !this.matchRuleForm.drawDetailEntities.length) {
			this.jsonApiService.message('请选择匹配规则');
			return
		}
		let reqdata = {
			drawDetailEntities: JSON.stringify(this.matchRuleForm.drawDetailEntities),
			useTypeL2: this.matchRuleForm.useTypeL2,
			componentType: this.matchRuleForm.componentType,
			id: this.matchRuleForm.id,
		}
		this.jsonApiService.http_obj("drawMatch/addDrawMath.html", reqdata).subscribe(data => {
			this.jsonApiService.message(data.message);
			if (data.success) {
				this.load();
				modal.hide()
			}
		})
	}
	// 关闭
	closeMatchRuleModal(modal) {
		modal.hide();
	}
	// 零件类型弹窗关闭
	closePartTypeModal(modal) {
		modal.hide();
	}
}